home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_10_05
/
1005042a
< prev
next >
Wrap
Text File
|
1992-03-07
|
4KB
|
148 lines
/* Listing 3 */
/*****************************************************
File Name: GS_JRD_F.C
Expanded Name: gauss-jordan fortran style
Global Function List: gauss_jordan
Portability: Standard C
******************************************************/
/* Standard Library */
#include <float.h>
#include <math.h>
#include <stdlib.h>
/* Types and Prototypes */
#include <matrix_t.h>
/* Own */
#include <gs_jrd.h>
/*****************************************************
Name: gauss_jordan
Parameters: A matrix of coeficients
n number of rows, cols in A
B matrix of constant vectors
m number of vectors, cols, in B
Return: SUCCESS, 0, or FAIL, -1, directly
Inverse of A in A indirectly
Solution vectors X in B indirectly
Description: Gauss-Jordan elimination with partial
pivoting is used to invert a matrix and
solve a system of linear equations.
The equations are expressed in the matrix
form as [A][X] = [B]. The A matrix is an
n by n matrix. The B matrix is an n by m
matrix. B is replaced with the solution
vectors X. A is replaced with the
inverse of A.
*****************************************************/
int gauss_jordan( matrix_t A, size_t n,
matrix_t B, size_t m )
{
size_t i, j, k, PivotRow;
double Pivot, Swap;
/* Loop through all the rows. */
for ( i = 0; i < n; i++ )
{
/* Set the default pivot to the
** current row. */
PivotRow = i;
Pivot = A[i][i];
/* Loop through the rows below the current
** row Looking for a large diagonal to
** pivot */
for ( j = i + 1; j < n; j++ )
{
/* Find the pivot row */
if ( fabs( A[j][i] ) >= fabs( Pivot ) )
{
PivotRow = j;
Pivot = A[j][i];
} /* if */
} /* for j */
/* Check to make sure we are not pivoting
** around the current row */
if ( PivotRow != i )
{
/* Swap the current row with the
** pivot row */
for ( j = 0; j < n; j++ )
{
Swap = A[i][j];
A[i][j] = A[PivotRow][j];
A[PivotRow][j] = Swap;
} /* for j */
for ( j = 0; j <= m; j++ )
{
Swap = B[i][j];
B[i][j] = B[PivotRow][j];
B[PivotRow][j] = Swap;
} /* for j */
} /* if PivotRow */
/* Check for a divide by zero error */
if ( fabs( Pivot ) <= DBL_MIN )
{
return ( -1 );
} /* if */
/* Invert the pivot so we can use * operator
** instead of / operator */
Pivot = 1.0 / Pivot;
/* Set the diagonal to 1.0 */
A[i][i] = 1.0;
/* Divide by the pivot */
for ( j = 0; j < n; j++ )
{
A[i][j] *= Pivot;
} /* for j */
/* Divide by the pivot */
for ( j = 0; j < m; j++ )
{
B[i][j] *= Pivot;
} /* for j */
/* Loop through all the rows doing row
** reduction */
for ( j = 0; j < n; j++ )
{
if ( j != i )
{
/* Reuse the Pivot variable as a
** dummy */
Pivot = A[j][i];
A[j][i] = 0.0;
/* Row reduce the matrix */
for ( k = 0; k < n; k++ )
{
A[j][k] -= A[i][k] * Pivot;
} /* for k */
/* Row reduce the solution vectors */
for ( k = 0; k < m; k++ )
{
B[j][k] -= B[i][k] * Pivot;
} /* for k */
} /* if j */
} /* for j */
} /* for i */
return ( 0 );
} /* function gauss_jordan */
/* End of File */